 <?php
/**
 * RegisterForm AR class file for users table.
 * ------------------------------------------
 *
 * LICENSE: BSD
 * @copyright  2009 Gemisoft
 * @author Deepak Pradhan <deepak.pradhan@gemisoft.com>
 * @link http://grbac.gemisoft.com/
 * @version $Id$
 * @since 0.1
 */

class RegisterForm extends CActiveRecord {
  /* @var string */
  public $passwordNew_repeat = '';

  /* @var string */
  public $passwordNew = '';

  /* @var boolean */
  public $rememberMe;

  /* @var string */
  public $namef;

  /* @var string */
  public $captcha;

  /* @var string */
  public $vLink = '';

  /**
   * Returns the static model of the specified AR class.
   * @return CActiveRecord the static model class
   */
  public static function model($className=__CLASS__) {
    return parent::model($className);
  }

  /**
   * @return string the associated database table name
   */
  public function tableName() {
    return 'users';
  }

  /**
   * @return array validation rules for model attributes.
   */
  public function rules() {
    return array(
    array('username','length','max'=>45),
    array('namef','length','max'=>20,'min'=>2),
    array('namel','length','max'=>20,'min'=>2),
    array('passwordNew','length','max'=>45, 'min'=>6),
    array('passwordNew_repeat','length','max'=>45, 'min'=>6),
    array('email','length','max'=>255),
    array('email','CEmailValidator','message'=>'יש להזין כתובת אימייל חוקית.'),
    array('passwordNew', 'compare', 'message'=>'הסיסמאות אינן תואמות'),
    array('active', 'numerical', 'integerOnly'=>true),

    //on insert (and register) check them all:
    array('username, passwordNew, passwordNew_repeat, email, namef, namel, center', 'required', 'on'=>array('insert', 'register'), 'message'=>'יש להזין ערך עבור  {attribute}.'),

    //on admin_register:
    array('username, passwordNew, passwordNew_repeat, email', 'required', 'on'=>array('admin_register')),

    //on register_passive:
    array('username, passwordNew, email', 'required', 'on'=>array('register_passive')),

    //on insert, update and register:
    array('email, username', 'unique', 'on'=>array('insert', 'update', 'register'),'message'=>'  קיים כבר במאגר {attribute} זהה.'),

    //on register
    array('captcha', 'captcha', 'allowEmpty'=>!extension_loaded('gd'), 'on'=>array('register'), 'message'=>'קוד האימות אינו נכון.'),
    );
  }


  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels() {
    return array(
    'username' => 'שם משתמש',
    'email' => 'אימייל',
    'passwordNew_repeat' => 'אימות סיסמה',
    'passwordNew' => 'סיסמה',
    'namef' => 'שם פרטי',
    'center' => 'סניף',
    'namel' => 'שם משפחה',
    'created' => 'נוצר',
    'lastlogin' => 'Last login',
    'rememberMe'=>'זכור אותי בפעם הבאה',
    'captcha' => 'קוד אימות',
    );
  }

  public function safeAttributes() {
    return array(
    'username, email, passwordNew, passwordNew_repeat, active, autosendInvite',
    'register' => 'username, email, namef, passwordNew, passwordNew_repeat, captcha',
    );
  }

  protected function beforeSave() {
    $algo = Yii::app()->getModule('grbac')->hashAlgo;
    // random salt =CCYYmmddHH, it must be set along with created, latter we will using created to derive the salt.
    $salt     = date("YmdH");
    $this->created = new CDbExpression('NOW()');
    $this->created = date("Y-m-d H:i:s");

    if (!function_exists('hash')) {
      $this->password = md5($this->passwordNew.$salt);
      $this->algo     = 'md5';
    }
    else {
      $this->password = hash($algo, $this->passwordNew.$salt);
      $this->algo     = $algo;
    }

    return parent::beforeSave();
  }

  protected function afterSave() {
  //assign default role to this new login if told so in configuration
    if($this->isNewRecord) {
      $defaultRole = Yii::app()->getModule('grbac')->defaultRole;
      if (($this->id) && (!empty($defaultRole))) {
        $auth = Yii::app()->authManager;
        $auth->assign($defaultRole, $userid=$this->id , $bizRules=null, $data=null);
      }
    }
    return parent::afterSave();
  }

  protected function beforeValidate() {
    if($this->isNewRecord) {
      if(Yii::app()->getModule('grbac')->emailConfirmation) { //outside registration, generate a validation code
        $this->active = 0;
        $this->valid_code = rand().rand().rand().rand();
      }
    }
    return true;
  }
}